package com.dofast.bt.mvp.presenter.shop;

import com.dofast.bt.base.BaseApplication;
import com.dofast.bt.base.BaseMvpPresenter;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.mvp.model.IMainModel;
import com.dofast.bt.mvp.model.MainModel;
import com.dofast.bt.mvp.view.activity.shop.TeaDetailView;
import com.dofast.bt.util.Helper;

import org.xutils.ex.DbException;

/**
 * username:Created by hsj on 2016/12/12 0012.
 * class：
 * 类描述：商品详情界面
 */

public class TeaDetailPresenter extends BaseMvpPresenter<TeaDetailView> implements ITeaDetailPresenter {
    private GoodsBean goodsBean = null;
    //addBasket：加入购物车
    private boolean addBasket = true;
    //collect：收藏
    private boolean collect = true;
    private int goodId;
    private IMainModel mainModel= null;
    public TeaDetailPresenter(){
        mainModel  = new MainModel();
    }

    @Override
    public void initData() {
        checkViewAttach();
        goodId = mvpView.getIntentData();
        initInfo();
    }
private void initInfo(){
    // TODO: 2016/12/28 0028 根据goodId获取数据库  GoodsBean
    try {
        GoodsBean goodsBean = mainModel.getGoodsById(goodId);
        if (goodsBean != null) {
            mvpView.initInfo(goodsBean,mainModel.isEnjoyed(goodId),mainModel.isAddCar(goodId));
        }
    } catch (DbException e) {
        e.printStackTrace();
    }
}

    @Override
    public void buy() {
        addToBasket(mvpView.getIntentData());
        mvpView.buy();
    }


    /**
     * 加入购物车
     */
    @Override
    public void intoBasket() {
        try {
            if (mainModel.isAddCar(mvpView.getIntentData())){
                removeFromBasket(mvpView.getIntentData());
            }else {
                addToBasket(mvpView.getIntentData());
            }
            initInfo();
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    /**
     * 加入收藏
     */
    @Override
    public void collect() {
        try {
            if (mainModel.isEnjoyed(mvpView.getIntentData())){
                removeFromCollect(mvpView.getIntentData());
            }else {
                addToCollect(mvpView.getIntentData());
            }
            initInfo();
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void showPopuWindow() {
        mvpView.showPopuwindow();
    }

    @Override
    public void addToBasket(int goodsId) {
        try {
            if (mainModel.addCar(goodsId)){
                Helper.showToast("加入购物车成功");
            }else {
                //Helper.showToast("操作失败");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeFromBasket(int goodsId) {
        try {
            if (mainModel.deleteCar(goodsId)){
                Helper.showToast("移除成功");
            }else {
                //Helper.showToast("操作失败");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }

    @Override
    public void addToCollect(int goodsId) {
        try {
            if (mainModel.enjoyGoods(goodsId)){
                Helper.showToast("收藏成功");
            }else {
               // Helper.showToast("操作失败");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void removeFromCollect(int goodsId) {
        try {
            if (mainModel.cancelEnjoyGoods(goodsId)){
                Helper.showToast("取消收藏成功");
            }else {
                //Helper.showToast("操作失败");
            }
        } catch (DbException e) {
            e.printStackTrace();
        }

    }
}
